package com.campsys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campsys.common.QueryPageParam;
import com.campsys.common.Result;
import com.campsys.entity.Activity;
import com.campsys.entity.User;
import com.campsys.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@RestController
@CrossOrigin(origins = "http://localhost:8080")
@RequestMapping({"/activity"})
public class ActivityController {
    @Autowired
    private ActivityService activityService;

    @PostMapping("/list")
    public Result listPage(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        Page<Activity> page =new Page<>();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Activity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        String name = (String) param.get("name");
        String category = (String) param.get("category");
        if (StringUtils.isNotBlank(name)){
            lambdaQueryWrapper.like(Activity::getName,name);
        }
        if (StringUtils.isNotBlank(category)){
            lambdaQueryWrapper.eq(Activity::getCategory,category);
        }

        IPage result =activityService.page(page,lambdaQueryWrapper);
        return Result.suc(result.getTotal(),result.getRecords());
    }

    @PostMapping("/save")
    public Result save(@RequestBody Activity activity){
        return activityService.save(activity)?Result.suc():Result.fail();
    }
    @PostMapping("/update")
    public Result update(@RequestBody Activity activity){
        return activityService.updateById(activity)?Result.suc():Result.fail();
    }
    @GetMapping("/del")
    public Result delete(@RequestParam Integer id){
        return activityService.removeById(id)?Result.suc():Result.fail();
    }

    @GetMapping("/getActivities")
    public List<Activity> getAllActivities() {
        return activityService.getAllActivities();
    }



}
